//
//  database.m
//  SmartRoadShow
//
//  Created by  ibokan on 10-9-25.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "database.h"
#import <PlausibleDatabase/PlausibleDatabase.h>
#define kDBPath @"SmartRoadShow.sqlite"

static PLSqliteDatabase *dbPointer;

@implementation database

//setup the database
+(PLSqliteDatabase *)setup
{
	if(dbPointer)
	{
		return dbPointer;
	}
    
	NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
	NSString *realPath = [documentPath stringByAppendingPathComponent:kDBPath];
	NSString *srcPath = [[NSBundle mainBundle] pathForResource:@"SmartRoadShow" ofType:@"sqlite"];
	
	NSFileManager *fileManager = [NSFileManager defaultManager];
	if(![fileManager fileExistsAtPath:realPath])
	{
		NSError *error;
		if(![fileManager copyItemAtPath:srcPath toPath:realPath error:&error])
		{
			NSLog(@"error is %@",[error localizedDescription]);
		}
	}
	NSLog(@"%@", realPath);
	dbPointer = [[PLSqliteDatabase alloc] initWithPath:realPath];
	BOOL re = [dbPointer open];
	NSLog(@"open? %d",re);
	
	return dbPointer;
}

//close the database
+(void)close
{
	if(dbPointer)
	{
		[dbPointer close];
		dbPointer = NULL;
	}
}


@end